import tkinter as tk
import tkinter.messagebox as msgbox
from collections import defaultdict

import customtkinter as ctk

from Attriabte import Color
from Attriabte.DTO import ColorHoverWidgetDto
from Entiy.WordGroup import WordGroup
from View.WindowComponent.BodyFrame.BodyFrame import BodyFrame
from View.WindowComponent.BodyFrame.EditWord import EditWord
from View.WindowComponent.ColorHoverLabel import ColorHoverLabel

wg = WordGroup()
show_frame = defaultdict(default_factory=lambda: False)


class ManagerFrame(BodyFrame):

    def group_block(self, groupname, add=False):
        frame = ctk.CTkFrame(self.frame, width=700, height=140)
        a = tk.Label(frame, text=groupname, font=('黑体', 16 if not add else 50), bg=Color.cutkinter)
        a.place(
            relwidth=0.8,
            relheight=0.8,
            relx=.1,
            rely=.1
        )

        frame.canvas.bind("<Button-1>", lambda e: self.event_frame_click(frame, groupname, add))
        a.bind("<Button-1>", lambda e: self.event_frame_click(frame, groupname, add))
        frame.bind("<Button-1>", lambda e: self.event_frame_click(frame, groupname, add))
        frame.pack(pady=12)

    def event_frame_click(self, wid, group, add):
        print("G")
        if add:
            print(f"新增->{group}")
            width, height = 300, 140
            if show_frame.get('add_btn'):
                self.div.destroy()
                show_frame['add_btn'] = False
            else:
                def _inner():
                    newname = self.entry_name.get()
                    wg.new_group(newname)
                    self.div.destroy()
                    self.layout.body(frame=self.get(), must_do=True)
                    show_frame['add_btn'] = False

                show_frame['add_btn'] = True

                self.div = tk.Frame(self.layout.win, bg=Color.light_green)

                self.entry_name = ctk.CTkEntry(self.div, placeholder_text='新增单词组名称', width=width - 40, height=40)
                self.entry_name.pack(
                    pady=20
                )

                frame = tk.Frame(self.div, width=width, bg=Color.light_green)
                frame.pack(
                    pady=20
                )

                colorHoverLabel_okay = ColorHoverLabel(
                    frame,
                    ColorHoverWidgetDto(
                        text='确认',
                        bg=Color.light_green,
                        hover_bg=Color.light_green,
                        hover_fg=Color.red
                    ),
                    lambda :_inner()
                )

                colorHoverLabel_cancel = ColorHoverLabel(
                    frame,
                    ColorHoverWidgetDto(
                        text='算了',
                        bg=Color.light_green,
                        hover_bg=Color.light_green,
                        hover_fg=Color.red
                    ),
                    lambda :self.event_frame_click(wid, group, add)
                )

                colorHoverLabel_okay.get().grid(
                    column=1, row=1, padx=5
                )

                colorHoverLabel_cancel.get().grid(
                    column=2, row=1, padx=5
                )

                self.div.place(
                    x=(self.layout.win.winfo_width() - width) / 2,
                    y=(self.layout.win.winfo_height() - height) / 2,
                    width=width,
                    height=height
                )

            return

        if self.switch.check_state:
            print(f"删除->{group}")
            if msgbox.askquestion("是否删除", f'是否删除词组{group} 该操作将不可逆转!') == 'no':
                return
            else:
                wg.remove_group(group)
                wid.destroy()

        else:
            print(f"编辑->{group}")
            self.layout.body(frame=EditWord(self.layout, False, group).get(), must_do=True)

    def event_switch(self, sw):

        if sw.check_state:
            sw.text_label['text'] = '删除模式: 点击单词组进行删除'
            sw.text_label['fg'] = Color.red
        else:
            sw.text_label['text'] = '编辑模式: 点击单词组进行编辑'
            sw.text_label['fg'] = Color.default_fg

    def pack(self):

        ctk.CTkLabel(self.frame, text="dfsdf ", text_font=('黑体', 16), width=700, height=140).pack()
        ctk.CTkLabel(self.frame, text="  | 管理 > 单词组", text_font=('黑体', 20), anchor=tk.W).place(
            width=700,
            height=140,
            x=0,
            y=0
        )

        self.switch = ctk.CTkSwitch(self.frame, text="编辑模式: 点击单词组进行编辑", text_font=('黑体', 14),
                                    command=lambda: self.event_switch(self.switch))
        self.switch.place(
            x=320,
            y=60
        )

        group_all = wg.get_all_group()
        print(group_all)
        for i in group_all:
            self.group_block(i)
        self.group_block("+ | 新增词组", True)


